from django.http import HttpResponse
from django.shortcuts import render, redirect
from utils.tencent.cos import delete_bucket
from web import models



def setting(request, project_id):
    """设置页面"""
    return render(request, "setting.html")

def setting_delete(request,project_id):
    """删除设置"""
    if request.method == "GET":
        return render(request, "setting_delete.html")
    project_name = request.POST.get("project_name")
    # 判断项目名是否存在，是否和当前项目的项目名相同
    if not project_name or project_name != request.tracer.project.title:
        return render(request, "setting_delete.html", {"error": "项目名错误"})

    # 项目名写对了则删除，（只有创建者才能删除）
    if request.tracer.user != request.tracer.project.creator:
        return render(request, "setting_delete.html", {"error": "只有项目创建者才能删除项目"})

    # 1. 删除桶
    # 删除桶中所有的文件
    # 删除桶中所有的碎片
    delete_bucket(
        request.tracer.project.bucket,
        request.tracer.project.region,
    )
    # 2. 删除项目
    models.Project.objects.filter(id=project_id).delete()
    return redirect("web:project_list")  # 返回项目列表